package com.tencent.wxcloudrun.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.tencent.wxcloudrun.config.ApiResponse;
import com.tencent.wxcloudrun.dto.CounterRequest;
import com.tencent.wxcloudrun.model.Counter;
import com.tencent.wxcloudrun.service.CounterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.*;

/**
 * counter控制器
 */
@RestController

public class CounterController {

  final CounterService counterService;
  final Logger logger;

  public CounterController(@Autowired CounterService counterService) {
    this.counterService = counterService;
    this.logger = LoggerFactory.getLogger(CounterController.class);
  }


  /**
   * 获取当前计数
   * @return API response json
   */
  @GetMapping(value = "/api/count")
  ApiResponse get() {
    logger.info("/api/count get request");
    Optional<Counter> counter = counterService.getCounter(1);
    Integer count = 0;
    if (counter.isPresent()) {
      count = counter.get().getCount();
    }

    return ApiResponse.ok(count);
  }


  /**
   * 更新计数，自增或者清零
   * @param request {@link CounterRequest}
   * @return API response json
   */
  @PostMapping(value = "/api/count")
  ApiResponse create(@RequestBody CounterRequest request) {
    logger.info("/api/count post request, action: {}", request.getAction());

    Optional<Counter> curCounter = counterService.getCounter(1);
    if (request.getAction().equals("inc")) {
      Integer count = 1;
      if (curCounter.isPresent()) {
        count += curCounter.get().getCount();
      }
      Counter counter = new Counter();
      counter.setId(1);
      counter.setCount(count);
      counterService.upsertCount(counter);
      return ApiResponse.ok(count);
    } else if (request.getAction().equals("clear")) {
      if (!curCounter.isPresent()) {
        return ApiResponse.ok(0);
      }
      counterService.clearCount(1);
      return ApiResponse.ok(0);
    } else {
      return ApiResponse.error("参数action错误");
    }
  }

  private static Map<String, String> map = new HashMap<>(16);

  static {
    map.put("王仪琳", "Y");
    map.put("王仪琳小笨蛋", "Y");
    map.put("我是猪", "Y");
    map.put("猪", "Y");
    map.put("小笨蛋", "Y");
    map.put("pig", "Y");
  }

  /**
   * 获取当前计数
   * @return API response json
   */
  @GetMapping(value = "/api/countUnSigned", produces = { "application/json;charset=UTF-8"})
  ApiResponse countUnSigned(String yourName,
                            String content) {
    logger.info("/api/countUnSigned get request");
    if (!(yourName.contains("pig") || yourName.contains("猪") || map.get(yourName) != null)) {
      ApiResponse.error("请输入你相关的种族");
    }
    String[] names = {"陈思琳" ,"戴钰珊" ,"董思祺" ,"何子琪" ,"黄蓝" ,"赖子涵" ,"李欣妍" ,"梁诗敏" ,"林书瑶" ,"刘栩帆" ,"卢佳倪" ,"聂梦羽" ,"饶羽彤" ,"唐思彤" ,"王若涵" ,"王子涵" ,"伍嘉怡" ,"闫梦琪" ,"虞子萱" ,"张林希" ,"张钰豪" ,"周钰媛" ,"曾栩妍" ,"蔡正炜" ,"陈耀扬" ,"程皓钊" ,"费致逸" ,"高安然" ,"黄天昱" ,"蒋雨辰" ,"李晨泽" ,"李玮峰" ,"李子沫" ,"林子涵" ,"刘清科" ,"刘震杰" ,"潘国锐" ,"孙涵" ,"孙早晨" ,"田博林" ,"韦子谦" ,"吴林远" ,"向泓瑞" ,"徐靖焱" ,"杨卓逸" ,"喻昊辰" ,"张仕铭" ,"赵辰宇" ,"周瑞安" ,"曾子坤"};
    List<String> upSigned = new ArrayList<>();
    for (String name : names) {
      if (!content.contains(name)){
        upSigned.add(name);
      }
    }
    logger.info(upSigned.toString());


    return ApiResponse.ok(upSigned.toString());
  }
  
}